package com.sharkburner.aeris.ui.module.site.component;

import java.io.Serializable;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sharkburner.aeris.ui.module.site.model.SourceListModel;
import com.sharkburner.common.listener.RepaintListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * @author adarie
 * 
 */
@Component
@Scope("prototype")
public class SourceListComponent extends VerticalLayout implements
		Serializable, RepaintListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private SourceListModel sourceListModel;
	private static final String PROPERTY_CHECKBOX = " ";
	private static final String PROPERTY_OPTIONS = "  ";
	private Table table;

	@PostConstruct
	public void init() {
		setWidth("100.0%");
		setHeight("100.0%");
		buildMainLayout();
	}

	private void buildMainLayout() {
		// initialization
		table = new Table();

		// draw table
		table.setWidth("370px");
		table.setHeight("100.0%");
		table.setSelectable(true);
		table.setImmediate(true);

		// turn on column reordering and collapsing
		table.setColumnReorderingAllowed(true);
		table.setColumnCollapsingAllowed(true);

		table.setColumnWidth(PROPERTY_CHECKBOX, 20);
		table.setColumnWidth(PROPERTY_OPTIONS, 20);
		repaint();

		// add table
		addComponent(table);
	}

	@Override
	public void repaint() {
		table.setContainerDataSource(sourceListModel.getData());
	}

	public Table getTable() {
		return table;
	}

}
